package com.nothing.controller;

import com.nothing.Dao.BannerDaoImpl;
import com.nothing.Dao.IBannerDao;
import com.nothing.Model.Banner;
import com.nothing.Service.BannerServiceImpl;
import com.nothing.Service.IBannerService;
import com.nothing.comm.Constant;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/3/23.
 */
@Controller
public class LunboAction {
    private IBannerService iBannerService=new BannerServiceImpl();
    @RequestMapping(value="/uploadfile",method=RequestMethod.GET)
    public String upLoadFile()
    {
        return "upload";
    }
    @RequestMapping(value="/uploadfile",method=RequestMethod.POST)
    public String upLoadFile(@RequestParam MultipartFile myfiles, HttpServletRequest request)
    {
                System.out.println("文件长度: " + myfiles.getSize());
                System.out.println("文件类型: " + myfiles.getContentType());
                System.out.println("文件名称: " + myfiles.getName());
                System.out.println("文件原名: " + myfiles.getOriginalFilename());
//                String realPath = request.getSession().getServletContext().getRealPath("/WEB-INF/upload");
                String realPath = request.getSession().getServletContext().getRealPath("/images/upload/banner");
//                request.getServletPath().
                //这里不必处理IO流关闭的问题，因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉，我是看它的源码才知道的
                try {
                    FileUtils.copyInputStreamToFile(myfiles.getInputStream(), new File(realPath + "/" +myfiles.getOriginalFilename()));
                } catch (IOException e) {
                    e.printStackTrace();
                    System.out.println("===================："+e.toString());
                }
//                http://192.168.0.128:8080/img/upload/a.jpg

                 Banner banner=new Banner();
                banner.setAd_image("/img/upload/banner/"+myfiles.getOriginalFilename());
                banner.setAd_name(myfiles.getName());
                iBannerService.addBanner(banner);
        //如果只是上传一个文件，则只需要MultipartFile类型接收文件即可，而且无需显式指定@RequestParam注解
        //如果想上传多个文件，那么这里就要用MultipartFile[]类型来接收文件，并且还要指定@RequestParam注解
        //并且上传多个文件时，前台表单中的所有<input type="file"/>的name都应该是myfiles，否则参数里的myfiles无法获取到所有上传的文件
//        for(MultipartFile myfile : myfiles)
//        {
//            if(myfile.isEmpty())
//            {
//                System.out.println("文件未上传"); continue;
//            }
//            else
//            {
//                System.out.println("文件长度: " + myfile.getSize());
//                System.out.println("文件类型: " + myfile.getContentType());
//                System.out.println("文件名称: " + myfile.getName());
//                System.out.println("文件原名: " + myfile.getOriginalFilename());
//                System.out.println("===================");
//
//              /*  //如果用的是Tomcat服务器，则文件会上传到-webapps-YourWebProject-WEB-INF-upload-文件夹中*/
//                String realPath = request.getSession().getServletContext().getRealPath("/WEB-INF/upload");
//                //这里不必处理IO流关闭的问题，因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉，我是看它的源码才知道的
//                try {
//                    FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File(realPath + "/" +myfile.getOriginalFilename()));
//                } catch (IOException e) {
//                    e.printStackTrace();
//                    System.out.println("===================："+e.toString());
//                }
//            }
//        }
//        System.out.println("========1122===========");
        return "success_fileupload";
    }

    @RequestMapping(value = "/banners",method = { RequestMethod.POST, RequestMethod.GET })
    public @ResponseBody List<Banner> getBanners( HttpServletRequest request,String name){
        List<Banner>  banners=iBannerService.findBanners();
        if(banners==null){return new ArrayList<Banner>();}
        if(banners.isEmpty()){return new ArrayList<Banner>();}
        for (int i = 0; i < banners.size(); i++) {
            Banner banner = banners.get(i);
            String str= Constant.BASE_URL+banner.getAd_image();
            banner.setAd_image(str);
        }
        return banners;
    }
}
